<?php
namespace common\models;


class Setting extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'setting';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['type', 'code'], 'required'],
            [['value'], 'string'],
            [['is_edit', 'create_time'], 'integer'],
            [['type', 'code', 'name'], 'string', 'max' => 32],
            [['describe'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'type' => '参数类型',
            'code' => '参数code',
            'name' => '参数名字',
            'value' => '参数值',
            'describe' => '参数描述',
            'is_edit' => '是否可以编辑',
            'create_time' => 'Create Time',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();
		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;

		foreach($extends as $extend)
		{

		}

		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

    ## 查询
    public static function getValue($type,$code)
    {
        $data = self::find()->where(['type'=>$type,'code'=>$code])->asarray()->one();
        return $data['value'];
    }


    ## 更新
    public static function setValue($type,$code,$value)
    {
        return self::updateAll(['value'=>$value],['type'=>$type,'code'=>$code]);
    }


}